package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.CourseWebDtoOrder;
import com.atguigu.eduorder.client.CourseClient;

import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2023-01-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private UcenterClient client;

    @Autowired
    private CourseClient courseClient;
    /**
     * 生成订单
     * @param courseId
     * @param id
     * @return
     */
    @Override
    public String createOrders(String courseId, String id) {

        Order order=new Order();

        //远程调用获取用户信息
        Map<String, String> infos = client.getInfos(id);
        logger.debug("infos为:{}",infos);

        order.setMemberId(infos.get("Id"));
        order.setMobile(infos.get("Mobile"));
        order.setNickname(infos.get("Nickname"));

        //远程调用获取课程信息
        CourseWebDtoOrder courseInfos = courseClient.getCourseInfos(courseId);
        logger.debug("courseInfos为:{}",courseInfos);
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfos.getTitle());
        order.setCourseCover(courseInfos.getCover());
        order.setTeacherName(courseInfos.getTeacherName());
        order.setTotalFee(courseInfos.getPrice());


        //生成订单号
        order.setOrderNo(OrderNoUtil.getOrderNo());

        order.setStatus(0);//订单状态，0未支付
        order.setPayType(1);//支付类型 微信1
        baseMapper.insert(order);


        //返回订单号
        return order.getOrderNo();
    }
}
